package ynu.edu.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;
import ynu.edu.common.entity.LogicEntity;

import javax.persistence.*;
import java.util.Objects;
import java.util.Set;

@Entity
@Getter
@Setter
public class Order1 extends LogicEntity {
    @Column
    private Double total;
    @Column
    private Integer status;
    @ManyToOne(fetch = FetchType.LAZY)
    @JsonIgnoreProperties(value = {"orders"})
    private Customer customer;
    @ManyToOne(fetch = FetchType.LAZY)
    @JsonIgnoreProperties(value = {"orders"})
    private Shop shop;
    @OneToMany(mappedBy = "order1",
            fetch = FetchType.LAZY,
            orphanRemoval = true,
            cascade = CascadeType.ALL)
    @JsonIgnoreProperties(value = {"order1"})
    private Set<LineItem> lineItems;
    public Double updateTotal(){
        total=0.0;
        for(LineItem l:lineItems){
            total+=l.getAmount()*l.getShopItem().getPrice();
        }
        return total;
    }
    public LineItem addLineItem(LineItem lineItem){
        System.out.println("size:"+lineItems.size());
        for(LineItem l:lineItems){
            if(Objects.equals(l.getShopItem().getId(), lineItem.getShopItem().getId())){
                l.setAmount(lineItem.getAmount());
                updateTotal();
                return l;
            }
        }
        System.out.println(lineItem.getShopItem().getId());
        lineItems.add(lineItem);
        updateTotal();
        return lineItem;
    }
}
